﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace Net.Fan.SmallPig.Common
{
    public class SN
    {

        /// <summary>
        /// 获取主板序号
        /// </summary>
        /// <returns></returns>
        private static string GetMotherBoardSerialNumber()
        {
            ManagementClass mc = new ManagementClass("WIN32_BaseBoard");
            ManagementObjectCollection moc = mc.GetInstances();
            string SerialNumber = null;
            foreach (ManagementObject mo in moc)
            {
                SerialNumber = mo["SerialNumber"].ToString();
                break;
            }
            return SerialNumber.Trim();
        }

        /// <summary>
        /// BIOS ID
        /// </summary>
        /// <returns></returns>
        private static string GetBIOSNumber()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select SerialNumber From Win32_BIOS");
            string biosNumber = null;
            foreach (ManagementObject mgt in searcher.Get())
            {
                biosNumber = mgt["SerialNumber"].ToString();
            }
            return biosNumber;
        }

        private static string GetHddId()
        {
            string HDid = null;
            ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                HDid = (string)mo.Properties["Model"].Value;
                break;
            }
            return HDid;
        }

        private static string GetCpuId()
        {
            string cpuInfo = null;//cpu序列号
            ManagementClass cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
            }
            return cpuInfo;
        }

        /// <summary>
        /// 取MAC地址
        /// </summary>
        /// <returns></returns>
        private static string GetNetCardMacAddress()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();

            string str = @"\";
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                    str = mo["MacAddress"].ToString();
            }
            return str.Trim();
        }

        public static string GetMachineId()
        {
            string sn = GetMotherBoardSerialNumber();
            if (sn.Length < 3)
            {
                sn = GetCpuId();
            }

            if (sn.Length < 3)
            {
                sn = GetHddId();
            }

            if (sn.Length < 3)
            {
                sn = GetNetCardMacAddress();
            }

            List<char> lc = new List<char>();
            foreach (var item in sn)
            {
                char c = Orderless(item);
                lc.Add(c);
            }

            List<char> c2 = new List<char>();
            for (int i = 0; i < lc.Count; i++)
            {
                c2.Add(lc[i]);

                if (i % 4 == 3)
                {
                    c2.Add('.');
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.Append(c2.ToArray());

            if (sb[sb.Length - 1] == '.')
            {
                sb.Remove(sb.Length - 1, 1);
            }

            return sb.ToString();
        }


        private static char Orderless(char c)
        {
            char[] ca = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '|', '[', ']', '{', '}', ' ', ';', ':', '"', ',', '.', '/', '\'', '\\' };
            char[] cb = new char[] { '4', 'n', 'l', 'm', '0', 'i', 'f', '1', 'e', 'k', 'a', 'h', 'j', 'o', 'w', 'p', 'q', 'p', 'u', 'v', '2', 'r', 's', 'x', '3', 'b', 'g', 'y', 'z', 'h', 't', '5', '6', 'g', 'c', 'd', 'i', 'j', 'k', '7', '8', 'n', 'o', 'e', '9', 's', 'x', 'y', 'd', 'l', 'm', 'z', '1', 't', 'r', '3', 'u', 'v', 'w', 'p', '2', 'f', 'a', 'b', 'c', 'p', 'q' };

            string s = c.ToString();
            s = s.ToLower();
            c = char.Parse(s);

            for (int i = 0; i < ca.Length; i++)
            {
                if (c == ca[i])
                {
                    return cb[i];
                }
            }

            return 'X';
        }

    }
}
